package ch.wanandroid.lib.banner;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import ch.wanandroid.comr.lib.R;

/**
 * @author chnehao
 * @datetime: 2018/10/9 16:39
 * @description:
 */
public abstract class BaseIndicatorBanner<E,T extends BaseIndicatorBanner<E,T>> extends BaseBanner<E,T> {

    /**
     * 指示器的样式，0为资源，1位为drawable的矩形
     */
    public static final int STYLE_DRAWABLE_RESOURCE = 0;
    public static final int STYLE_CORNER_RECTANGLE = 1;
    private int mIndicatorStyle;
    private int mIndicatorWidth;
    private int mIndicatorHeight;
    private int mIndicatorGap;
    private ArrayList<ImageView> mIndicatorViews = new ArrayList<>();
    Drawable mSelectDrawable;
    Drawable mUnSelectDrawable;
    int mIndicatorCornerRadius;
    int mSelectColor;
    int mUnselectColor;
    /**
     * 指示器直接容器
     */
    LinearLayout mLlIndicators;
    private Class<? extends BaseAnimator> mSelectAnimClass;
    private Class<? extends BaseAnimator> mUnselectAnimClass;
    public BaseIndicatorBanner(Context context) {
        this(context, null, 0);
    }

    public BaseIndicatorBanner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseIndicatorBanner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseIndicatorBanner);
        mIndicatorStyle = ta.getInt(R.styleable.BaseIndicatorBanner_bb_indicatorStyle, STYLE_CORNER_RECTANGLE);
        mIndicatorWidth = ta.getDimensionPixelSize(R.styleable.BaseIndicatorBanner_bb_indicatorWidth, dp2px(6));
        mIndicatorHeight = ta.getDimensionPixelSize(R.styleable.BaseIndicatorBanner_bb_indicatorHeight, dp2px(6));
        mIndicatorGap = ta.getDimensionPixelSize(R.styleable.BaseIndicatorBanner_bb_indicatorGap, dp2px(6));
        mIndicatorCornerRadius = ta.getDimensionPixelSize(R.styleable.BaseIndicatorBanner_bb_indicatorCornerRadius, dp2px(3));
        //选中时白色
        mSelectColor = ta.getColor(R.styleable.BaseIndicatorBanner_bb_indicatorSelectColor, Color.parseColor("#ffffff"));
        //没选中时透明灰色
        mUnselectColor = ta.getColor(R.styleable.BaseIndicatorBanner_bb_indicatorUnselectColor, Color.parseColor("#88ffffff"));
        int selectRes = ta.getResourceId(R.styleable.BaseIndicatorBanner_bb_indicatorSelectRes, 0);
        int unselectRes = ta.getResourceId(R.styleable.BaseIndicatorBanner_bb_indicatorUnselectRes, 0);
        ta.recycle();
        mLlIndicators = new LinearLayout(context);
        mLlIndicators.setGravity(Gravity.CENTER);
        //图片资源设置指示器样式
        setIndicatorSelectorRes(unselectRes, selectRes);
    }



    @Override
    public View onCreateIndicator() {
        //rectangle
        this.mUnSelectDrawable = getDrawable(mUnselectColor, mIndicatorCornerRadius);
        this.mSelectDrawable = getDrawable(mSelectColor, mIndicatorCornerRadius);


        int size = mDatas.size();
        mIndicatorViews.clear();

        mLlIndicators.removeAllViews();
        for (int i = 0; i < size; i++) {
            ImageView iv = new ImageView(mContext);
            iv.setImageDrawable(i == dataPosition ? mSelectDrawable : mUnSelectDrawable);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mIndicatorWidth,
                    mIndicatorHeight);
            lp.leftMargin = i == 0 ? 0 : mIndicatorGap;
            mLlIndicators.addView(iv, lp);
            mIndicatorViews.add(iv);
        }

        setCurrentIndicator(dataPosition);

        return mLlIndicators;
    }

    @Override
    public void setCurrentIndicator(int position) {
        for (int i = 0; i < mIndicatorViews.size(); i++) {
            mIndicatorViews.get(i).setImageDrawable(i == position ? mSelectDrawable : mUnSelectDrawable);
        }
        //切换动画
        try {
            if (mSelectAnimClass != null) {
                //刚开始position==0
                if (position == mLastPositon) {
                    mSelectAnimClass.newInstance().start(mIndicatorViews.get(position));
                } else {
                    mSelectAnimClass.newInstance().start(mIndicatorViews.get(position));
                    if (mUnselectAnimClass == null) {
                        mSelectAnimClass.newInstance().interpolator(new ReverseInterpolator()).start(mIndicatorViews.get(mLastPositon));
                    } else {
                        mUnselectAnimClass.newInstance().start(mIndicatorViews.get(mLastPositon));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 class ReverseInterpolator implements Interpolator{

     @Override
     public float getInterpolation(float input) {
         return 1-input;
     }
 }
    /** 设置显示宽度,单位dp,默认6dp */
    public T setIndicatorWidth(float indicatorWidth) {
        this.mIndicatorWidth = dp2px(indicatorWidth);
        return  (T)this;
    }

    /** 设置显示器高度,单位dp,默认6dp */
    public T setIndicatorHeight(float indicatorHeight) {
        this.mIndicatorHeight = dp2px(indicatorHeight);
        return  (T)this;
    }

    /**指示器选中动画
     * @param mSelectAnimClass
     * @return
     */
    public T setSelectAnim(Class<? extends BaseAnimator> mSelectAnimClass){
        this.mSelectAnimClass=mSelectAnimClass;
        return (T)this;
    }

    /**指示器未选中动画
     * @param mUnselectAnimClass
     * @return
     */
    public T setUnSelectAnim(Class<? extends BaseAnimator> mUnselectAnimClass){
        this.mUnselectAnimClass=mUnselectAnimClass;
        return (T)this;
    }

    /** 设置两个显示器间距,单位dp,默认6dp */
    public T setIndicatorGap(float indicatorGap) {
        this.mIndicatorGap = dp2px(indicatorGap);
        return (T) this;
    }

    /** 设置显示器选中颜色(for STYLE_CORNER_RECTANGLE),默认"#ffffff" */
    public T setIndicatorSelectColor(int selectColor) {
        this.mSelectColor = selectColor;
        return (T) this;
    }

    /** 设置显示器未选中颜色(for STYLE_CORNER_RECTANGLE),默认"#88ffffff" */
    public T setIndicatorUnselectColor(int unselectColor) {
        this.mUnselectColor = unselectColor;
        return (T) this;
    }

    /** 设置显示器圆角弧度(for STYLE_CORNER_RECTANGLE),单位dp,默认3dp */
    public T setIndicatorCornerRadius(float indicatorCornerRadius) {
        this.mIndicatorCornerRadius = dp2px(indicatorCornerRadius);
        return (T) this;
    }

    /** 设置显示器选中以及未选中资源(for STYLE_DRAWABLE_RESOURCE) */
    public T setIndicatorSelectorRes(int unselectRes, int selectRes) {
        try {
                if (selectRes != 0) {
                    this.mSelectDrawable = getResources().getDrawable(selectRes);
                }
                if (unselectRes != 0) {
                    this.mUnSelectDrawable = getResources().getDrawable(unselectRes);
                }
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return (T) this;
    }

    private GradientDrawable getDrawable(int color, float raduis) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(raduis);
        drawable.setColor(color);
        return drawable;
    }

}
